const app = getApp();
const common = require('../../utils/common.js');
const api = require('../../config/api.js');

Page({
  data: {
    imgUrl: app.globalData.imgurl,
    currentIndex: 0,
    keyword: ''
  },
  onLoad: function (options) {

  },
  onShow: function () {
    this.setData({
      keyword: ''
    });
    this.getIndexData();
  },
  getIndexData: function () {
    let that = this;
    common.request(api.ProductIndexInfo, {}, 'get').then(function (data) {
      if (data.type === 'success') {
        that.setData({
          adList: data.data.adList,
          categoryList: data.data.categoryList,
          jingpinList: data.data.jingpinList,
          huodongList: data.data.huodongList
        });
      }
    });
  },
  keywordInput: function (e) {
    var that = this;
    that.setData({
      keyword: e.detail.value
    });
  },
  onKeywordConfirm: function () {
    var keyword = this.data.keyword;
    wx.navigateTo({
      url: '../category/category?keyword=' + keyword
    })
  },
  toCategory: function (e) {
    var parentCategoryId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/category/category?categoryId=' + parentCategoryId
    }) 
    // wx.navigateTo({
    //   url: '../catalog/catalog?parentCategoryId=' + parentCategoryId
    // }) 
  },
  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  tel: function () {
    wx.makePhoneCall({
      phoneNumber: app.globalData.phoneNumber,
    })
  },
  onShareAppMessage: function () {
    return {
      title: '瓯客会',
      desc: '瓯客会',
      path: '/pages/index2/index2'
    }
	},
	link: function (e) {
		var linkType = e.currentTarget.dataset.linkType;
		var linkId = e.currentTarget.dataset.linkId;
		if (linkId != null && linkId != '') {
			if (linkType == 1) {
				wx.navigateTo({
					url: '/pages/newsDetail/newsDetail?articleId=' + linkId
				})
			} else if (linkType == 2) {
				wx.navigateTo({
					url: '/pages/goods/goods?productSkuId=' + linkId
				})
			}
		}
	}
})
